package com.you;

import java.util.Timer;
import java.util.TimerTask;

public class Text {
    Timer timer = new Timer(); // 创建定时器
    private int speed = 100; // 速度
    int nowStr = 0; // 当前字的坐标
    String str = "你好，陌生人";

    public static void main(String[] args) {
        new Text().textJempOut();
    }

    // 文字蹦出方法
    private void textJempOut() {
        char[] chars = new char[str.length()];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = str.charAt(i);
        }
        // 计时器
        TimerTask write = new TimerTask(){

            @Override
            public void run() {
                if (nowStr == chars.length){
                    timer.cancel();
                } else {
                    System.out.println(chars[nowStr]);
                    nowStr++;
                }
            }
        };
        // 按照一定时间运行
        timer.schedule(write,0,speed);
    }
}
